package com.wrh.springbootstudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wrh.springbootstudy.bean.UserBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Api(tags = "使用@PostMapping")
@RestController
@RequestMapping("/PostMapping")
public class PostMappingController {

    @ApiOperation(value = "测试@PostMapping", notes = "POST把提交的数据则放置在HTTP包的包体中")
    @PostMapping("/test1")
    public String test1(String user, String pwd) {
        return "test2：my user is " +
                user +
                " and my pwd is " +
                pwd +
                "\n" +
                "POST把提交的数据则放置在HTTP包的包体中";
    }


    @ApiOperation(value = "Post接收json 使用@RequestBody", notes = "通过@RequestBody 的方式，直接将json的数据注入到了JSONObject里面")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "JSON数据", name = "params", paramType = "query", required = true,
                    defaultValue = "{\n" +
                            "  \"province\": \"湖北省\",\n" +
                            "  \"town\": \"\",\n" +
                            "  \"stationNo\": \"57249\",\n" +
                            "  \"city\": \"十堰市\",\n" +
                            "  \"stationLevel\": \"13\",\n" +
                            "  \"adminCode\": \"420324\",\n" +
                            "  \"stationName\": \"竹溪\",\n" +
                            "  \"lon\": \"109.68\",\n" +
                            "  \"cnty\": \"竹溪县\",\n" +
                            "  \"lat\": \"32.32\"\n" +
                            "}")
    })
    @PostMapping("/test2")
    public JSONObject test2(@RequestBody JSONObject params) {
        return params;
    }

    @ApiOperation(value = "Post接收json 使用HttpServletRequest", notes = "通过request的对象来获取到输入流，然后将输入流的数据写入到字符串里面，最后转化为JSON对象")
    @PostMapping("/test3")
    public JSONObject test3(HttpServletRequest request) {
        JSONObject jsonParam = null;
        try {
            BufferedReader streamReader  = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }


    @ApiOperation(value = "Post 使用实体类对象接收数据", notes = "通过@RequestBody 的方式，直接将json的数据注入到了JSONObject里面")
    @PostMapping("/test4")
    public UserBean test4(@RequestBody UserBean userBean) {
        return userBean;
    }


    @ApiOperation(value = "Post 使用实体类对象集合接收数据数组", notes = "通过@RequestBody 的方式，直接将json的数据注入到了JSONObject里面")
    @PostMapping("/test5")
    public List<UserBean> test5(@RequestBody List<UserBean> userBeanList) {
        return userBeanList;
    }

}
